﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using System.Data.SqlClient;
using System.Globalization;

public partial class order : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["pnr"] == null)
            {
                lblError.Text = "非法调用！";
                return;
            }
            txtOrderNo.Text = Request.QueryString["pnr"];
            btnRefresh_Click(sender, e);
        }
        btnCancel.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");        
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        CRSI.Order instOrder=new CRSI.Order();
        int ret_code;
        string ret_msg;
        string tmp;
        //float adt_price,chd_price,inf_price,
        StringBuilder buf=new StringBuilder();
        DataSet dsRet;
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Convert.ToInt64(txtOrderNo.Text).ToString() + "|" + Application["HASH_PWD"].ToString(), "MD5");
        dsRet = instOrder.GetCommonInfo(Convert.ToInt64(txtOrderNo.Text), Application["UID"].ToString(), sign,out ret_code, out ret_msg);
        if (ret_code < 0)
        {
            lblError.Text = ret_msg;
            return;
        }
        if (ret_code > 0)
        {
            switch (ret_code)
            {
                case 1:
                    lblError.Text = "订单找不到";
                    break;
                case 2:
                    lblError.Text = "无权查看";
                    break;
                case 3:
                    lblError.Text = "无查看本Office订单权限";
                    break;
                case 4:
                    lblError.Text = "无查看本Entity订单权限";
                    break;
                case 91:
                    lblError.Text = "登录错";
                    break;
                default:
                    lblError.Text = "未知错误：" + ret_code.ToString();
                    break;
            }
            return;
        }
        lblError.Text = "";
        DataRow row = dsRet.Tables[0].Rows[0];
        switch (row["status"].ToString())
        {
            case "P":
                tmp = "未提交";
                btnPay.Visible = false;
                btnCancel.Visible = true;
                break;
            case "S":
                tmp = "尚未支付！请按支付按钮";
                btnPay.Visible = true;
                btnCancel.Visible = true;
                break;
            case "A":
                //redict to tickets.aspx??
                Response.Redirect("tickets.aspx?pnr=" + txtOrderNo.Text);
                tmp = "已支付。请打印行程单";
                btnPay.Visible = false;
                btnCancel.Visible = false;
                break;
            case "X":
                tmp = "已取消";
                btnPay.Visible = false;
                btnCancel.Visible = false;
                break;
            default:
                tmp = "非法状态！";
                btnPay.Visible = false;
                btnCancel.Visible = false;
                break;
        }
        txtStatus.Text = tmp;
        txtContact.Text = row["contact_man"].ToString();
        txtMobile.Text = row["mobile_phone"].ToString();
        txtPhone.Text = row["phone"].ToString();
        txtEmail.Text = row["email"].ToString();
        txtPayBefore.Text = row["pay_before"].ToString();
        if(Convert.IsDBNull(row["pay_time"]))
        {
            txtPaidTotal.Text="<未支付>";
            txtPaidTime.Text="";
        }
        else
        {
            txtPaidTotal.Text = row["pay_currency"].ToString() + row["total_fare"].ToString() ;
            txtPaidTime.Text=row["pay_time"].ToString();
        }
        dsRet.Clear();
        //////////////////////////////////////////////////////////////////////////////////////////
        dsRet = instOrder.GetTrip(Convert.ToInt64(txtOrderNo.Text),2, Application["UID"].ToString(), sign, out ret_code, out ret_msg);
        if (ret_code < 0)
        {
            lblError.Text = ret_msg;
            return;
        }
        if (ret_code > 0)
        {
            switch (ret_code)
            {
                case 1:
                    lblError.Text = "订单找不到";
                    break;
                case 2:
                    lblError.Text = "无权查看";
                    break;
                case 3:
                    lblError.Text = "无查看本Office订单权限";
                    break;
                case 4:
                    lblError.Text = "无查看本Entity订单权限";
                    break;
                case 91:
                    lblError.Text = "登录错";
                    break;
                default:
                    lblError.Text = "未知错误：" + ret_code.ToString();
                    break;
            }
            return;
        }
        lblError.Text = "";
        lstTripSn.Items.Clear();
        bool found = false;
        ListItem i=new ListItem();
        buf.Append("<table><tr><th>日期</th><th>航程</th><th>航班号</th><th>舱位</th><th>成人单价(不含税/费)</th><th>儿童单价(不含税/费)</th><th>婴儿单价(不含税/费)</th></tr>");
        foreach(DataRow row2 in dsRet.Tables[0].Rows)
        {
            tmp=row2["fare_currency"].ToString();
            buf.Append("<tr><td>"+row2["dep_time"].ToString()+"</td><td>"+row2["ori_airport"]+"-"+row2["des_airport"]+"</td><td>" 
                +row2["airline"]+row2["flt_no"]+"</td><td>"+row2["cabin"]+"/"+row2["class"]+"</td><td>"+tmp+row2["final_price"].ToString()
                +"</td><td>"+tmp+row2["chd_price"].ToString()+"</td><td>"+tmp+row2["inf_price"].ToString()+"</td></tr>");
            i.Text=row2["trip_sn"].ToString();
            i.Value=i.Text;
            foreach (ListItem item in lstTripSn.Items)//如果下拉列表中有重复值，会产生错误！
            {
                if (item.Value == i.Value)
                    found = true;
                
            }
            if(!found)
                lstTripSn.Items.Add(i);
        }
        buf.Append("</table>");
        litTrip.Text=buf.ToString();
        buf.Remove(0,buf.Length);
        dsRet.Clear();
        lstTripSn_SelectedIndexChanged(sender, e);
        //////////////////////////////////////////////////////////////////////////////////////////
        string age, inf_dob;
        dsRet = instOrder.GetPaxList(Convert.ToInt64(txtOrderNo.Text), Application["UID"].ToString(), sign, out ret_code, out ret_msg);
        if (ret_code < 0)
        {
            lblError.Text = ret_msg;
            return;
        }
        if(ret_code>0)
        {
            switch(ret_code)
            {
                case 1:
                    lblError.Text="订单找不到";
                    break;
                case 2:
                    lblError.Text="无权查看";
                    break;
                case 3:
                    lblError.Text="无查看本Office订单权限";
                    break;
                case 4:
                    lblError.Text="无查看本Entity订单权限";
                    break;
                case 91:
                    lblError.Text="登录错";
                    break;
                default:
                    lblError.Text="未知错误："+ret_code.ToString();
                    break;
            }
            return;
        }
        lblError.Text = "";
        buf.Append("<table><tr><th>PAX_SN</th><th>姓名</th><th>姓名(英文)</th><th>性别</th><th>VIP</th><th>备注</th><th>年龄</th><th>生日</th><th>国籍</th><th>证件</th><th>证件号</th><th>证件到期日</th><th>电话</th><th>电子邮件</th><th>常客号码</th></tr>");
        foreach (DataRow row3 in dsRet.Tables[0].Rows)
        {
            buf.Append("<tr><td>"+row3["pax_sn"].ToString()+"</td><td>" + row3["family_name"] + "/" + row3["name"] + "</td><td>" + row3["family_name_en"] + "/" + row3["name_en"] + "</td>");
            if(Convert.ToBoolean(row3["male"]))
                buf.Append("<td>男</td>");
            else
                buf.Append("<td>女</td>");
            if (Convert.ToBoolean(row3["vip"]))
                buf.Append("<td>VIP</td>");
            else
                buf.Append("<td></td>");
            buf.Append("<td>" + row3["remark"] + "</td>");
            age = row3["age_type"].ToString();
            if ("A" == age)
                buf.Append("<td>成人</td>");
            else if ("C" == age)
                buf.Append("<td>儿童</td>");
            else if ("I" == age)
                buf.Append("<td>婴儿</td>");
            else
                buf.Append("<td>"+age+"</td>");
            if (Convert.IsDBNull(row3["inf_dob"]))
                inf_dob = "";
            else
                inf_dob = Convert.ToDateTime(row3["inf_dob"]).ToShortDateString();
            buf.Append("<td>" + inf_dob + "</td><td>" + row3["nationality"] + "</td><td>" + row3["id_type"] + "</td><td>" + row3["id_no"] + "</td><td>" + Convert.ToDateTime(row3["id_expired_date"]).ToShortDateString() + "</td><td>" + row3["mobile_phone"] + "</td><td>" + row3["email"] + "</td><td>" + row3["ffp_airline"] + row3["ffp_id"] + "</td></tr>");

        }
        buf.Append("</table>");
        litPax.Text = buf.ToString();
        
        //////////////////////////////////////////////////////////////////////////////////////////
        retreive_emd();
    }

    protected void retreive_emd()
    {
        CRSI.Order instOrder = new CRSI.Order();
        int ret_code;
        string ret_msg;
        string tmp;
        //float adt_price,chd_price,inf_price,
        StringBuilder buf = new StringBuilder();
        DataSet dsRet;
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Convert.ToInt64(txtOrderNo.Text).ToString() + "|" + Application["HASH_PWD"].ToString(), "MD5");
        dsRet = instOrder.GetEmd(Convert.ToInt64(txtOrderNo.Text),2, Application["UID"].ToString(), sign, out ret_code, out ret_msg);
        if (ret_code < 0)
        {
            lblError.Text = ret_msg;
            return;
        }
        if (ret_code > 0)
        {
            switch (ret_code)
            {
                case 91:
                    lblError.Text = "登录错";
                    break;
                default:
                    lblError.Text = "未知错误：" + ret_code.ToString();
                    break;
            }
            return;
        }
        buf.Append("<table><tr><th></th><th>旅客序号</th><th>航程序号</th><th>服务编号</th><th>服务名称</th><th>单价</th><th>数量</th><th>备注</th></tr>");
        foreach (DataRow row3 in dsRet.Tables[0].Rows)
        {
            buf.Append("<tr><td><input type='radio' name='emdKEY' value='" + row3["bk_id"].ToString() + "|" + row3["pax_sn"].ToString() + "|" + row3["cart_time"].ToString() + "'></td><td>" + row3["pax_sn"].ToString() + "</td><td>" + row3["trip_sn"].ToString() + "</td>");
            buf.Append("<td>" + row3["emd_airline"]+"/"+row3["emd_id"] + "</td><td>"+row3["emd_name"]+"</td><td>"+row3["currency"]+row3["price"].ToString()+"</td><td>"+row3["quantity"].ToString()+"</td><td>"+row3["remark"]+"</td></tr>");
        }
        buf.Append("</table>");
        litEmd.Text = buf.ToString();
    }
    protected void btnPay_Click(object sender, EventArgs e)
    {
        Response.Redirect("pay_order.aspx?pnr=" + txtOrderNo.Text);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        CRSI.Order instOrder = new CRSI.Order();
        int ret_code = 0;
        string ret_msg;
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Convert.ToInt64(txtOrderNo.Text).ToString() + "|" + Application["HASH_PWD"].ToString(), "MD5");
        ret_code = instOrder.Cancel(Convert.ToInt64(txtOrderNo.Text), Application["UID"].ToString(), sign, out ret_msg);
        if (ret_code < 0)
        {
            lblError.Text = ret_msg;
            return;
        }
        switch (ret_code)
        {
            case 0:
                //update local order status
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "cancel_order";
                SqlParameter para = cmd.Parameters.AddWithValue("@uid", Session["UID"].ToString());
                para = cmd.Parameters.AddWithValue("@order_no", txtOrderNo.Text);
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code != 0)
                {
                    lblError.Text = "本地订单删除失败，实际订单已取消。";
                    return;
                }
                ////RetreivePaxList();
                lblError.Text = "订单已取消。";
                break;
            case 1:
                lblError.Text = "取消失败：1";
                break;
            case 2:
                lblError.Text = "取消失败：2";
                break;
            case 3:
                lblError.Text = "取消失败：3";
                break;
            case 4:
                lblError.Text = "取消失败：4";
                break;
            case 5:
                lblError.Text = "找不到订单";
                break;
            case 6:
                lblError.Text = "无权限";
                break;
            case 81:
                lblError.Text = "取消失败：81";
                break;
            case 90:
                lblError.Text = "取消失败：90";
                break;
            case 91:
                lblError.Text = "登录错";
                break;
            case 150:
                lblError.Text = "取消失败：150";
                break;
            default:
                lblError.Text = "未知错误：" + ret_code.ToString();
                break;
        }
    }

    protected void lstEntities_SelectedIndexChanged(object sender, EventArgs e)
    {
        //CRSI.Order instOrder = new CRSI.Order();
        //int ret_code;
        //string ret_msg;
        ////float adt_price,chd_price,inf_price,
        //StringBuilder buf = new StringBuilder();
        //DataSet dsRet;
        ////string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        //string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(lstEntities.SelectedValue + "|" + Application["HASH_PWD"].ToString(), "MD5");
        //dsRet = instOrder.GetEmdIDs(lstEntities.SelectedValue, Application["UID"].ToString(), sign, out ret_code, out ret_msg);
        //if (ret_code < 0)
        //{
        //    lblError.Text = ret_msg;
        //    return;
        //}
        //if (ret_code > 0)
        //{
        //    switch (ret_code)
        //    {
        //        case 91:
        //            lblError.Text = "登录错";
        //            break;
        //        default:
        //            lblError.Text = "未知错误：" + ret_code.ToString();
        //            break;
        //    }
        //    return;
        //}
        //lblError.Text = "";
        //ListItem i;
        //lstEmdID.Items.Clear();
        //foreach (DataRow row2 in dsRet.Tables[0].Rows)
        //{
        //    i = new ListItem();
        //    i.Text = row2["name"].ToString();
        //    i.Value = row2["id"].ToString();
        //    lstEmdID.Items.Add(i);
        //}
        //dsRet.Clear();
    }
    protected void btnRetreiveMerchan_Click(object sender, EventArgs e)
    {
        CRSI.Order instOrder = new CRSI.Order();
        int ret_code;
        string ret_msg;
        string tmp;
        //float adt_price,chd_price,inf_price,
        StringBuilder buf = new StringBuilder();
        DataSet dsRet;
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtOrderNo.Text + "|" +lstTripSn.SelectedValue+"|"+ lstEmdID.SelectedValue + "|" + Application["HASH_PWD"].ToString(), "MD5");
        dsRet = instOrder.GetEmdFare(Convert.ToInt64(txtOrderNo.Text),Convert.ToInt16(lstTripSn.SelectedValue),lstEmdID.SelectedValue,2, Application["UID"].ToString(), sign, out ret_code, out ret_msg);
        if (ret_code < 0)
        {
            lblError.Text = ret_msg;
            return;
        }
        if (ret_code > 0)
        {
            switch (ret_code)
            {
                case 91:
                    lblError.Text = "登录错";
                    break;
                default:
                    lblError.Text = "未知错误：" + ret_code.ToString();
                    break;
            }
            return;
        }
        lblError.Text = "";

        lblCurrency.Text = dsRet.Tables[0].Rows[0]["currency"].ToString();
        lblPrice.Text = dsRet.Tables[0].Rows[0]["price"].ToString();
        txtEmdNote.Text = dsRet.Tables[0].Rows[0]["emd_note"].ToString();
        lblUnit.Text = dsRet.Tables[0].Rows[0]["unit"].ToString();
    }
    protected void BtnAddToCart_Click(object sender, EventArgs e)
    {
        CRSI.Order instOrder = new CRSI.Order();
        int ret_code = 0;
        string ret_msg;
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        string buf;
        buf = Convert.ToInt64(txtOrderNo.Text).ToString() + "|" + Convert.ToInt16(txtEmdPaxSn.Text).ToString() + "|" + Convert.ToInt16(lstTripSn.SelectedValue).ToString()+"|"
            + lstEmdID.SelectedValue + "|" + Convert.ToSingle(txtQuantity.Text).ToString()+"|"+txtEmdRemark.Text+"|"
            + Application["HASH_PWD"].ToString();
        string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(buf, "MD5");
        ret_code = instOrder.EmdAddToCart(Convert.ToInt64(txtOrderNo.Text), Convert.ToInt16(txtEmdPaxSn.Text), Convert.ToInt16(lstTripSn.SelectedValue), lstEmdID.SelectedValue, Convert.ToSingle(txtQuantity.Text), txtEmdRemark.Text,
            Application["UID"].ToString(), sign, out ret_msg);
        if (ret_code < 0)
        {
            lblError.Text = ret_msg;
            return;
        }
        switch (ret_code)
        {
            case 0:
                //refresh emd gridview...
                retreive_emd();
                lblError.Text = "Done.";
                break;
            case 1:
                lblError.Text = "Order not found!";
                break;
            case 2:
                lblError.Text = "Pax not found!";
                break;
            case 3:
                lblError.Text = "EMD not found, or mismatched!";
                break;
            case 4:
                lblError.Text = "Adding failed!";
                break;
            case 91:
                lblError.Text = "登录错";
                break;
            default:
                lblError.Text = "未知错误：" + ret_code.ToString();
                break;
        }
    }
    protected void btnRemoveFromCart_Click(object sender, EventArgs e)
    {
        string key;
        string[] strArray;
        if (Request.Form["emdKEY"] == null)
        {
            lblError.Text = "Please select the ticket.";
            return;
        }
        key = Request.Form["emdKEY"];
        strArray = key.Split('|');

        CRSI.Order instOrder = new CRSI.Order();
        int ret_code = 0;
        string ret_msg;
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        string buf;
        buf = Convert.ToInt64(strArray[0]).ToString() + "|" + Convert.ToInt16(strArray[1]).ToString() + "|" + Convert.ToDateTime(strArray[2]).ToString("yyyy-MM-dd HH:mm:ss",DateTimeFormatInfo.InvariantInfo) + "|"
            + Application["HASH_PWD"].ToString();
        string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(buf, "MD5");
        ret_code = instOrder.EmdRemoveFromCart(Convert.ToInt64(strArray[0]), Convert.ToInt16(strArray[1]), Convert.ToDateTime(strArray[2]),
            Application["UID"].ToString(), sign, out ret_msg);
        if (ret_code < 0)
        {
            lblError.Text = ret_msg;
            return;
        }
        switch (ret_code)
        {
            case 0:
                //refresh emd gridview...
                retreive_emd();
                lblError.Text = "Done.";
                break;
            case 1:
                lblError.Text = "Failed to remove!";
                break;
            case 2:
                lblError.Text = "Check order status!";
                break;
            case 91:
                lblError.Text = "登录错";
                break;
            default:
                lblError.Text = "未知错误：" + ret_code.ToString();
                break;
        }
    }
    protected void lstTripSn_SelectedIndexChanged(object sender, EventArgs e)
    {
        CRSI.Order instOrder = new CRSI.Order();
        int ret_code;
        string ret_msg;
        //float adt_price,chd_price,inf_price,
        StringBuilder buf = new StringBuilder();
        DataSet dsRet;
        //string pwd_sha1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Application["PWD"].ToString(), "SHA1");
        string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtOrderNo.Text+"|"+lstTripSn.SelectedValue + "|" + Application["HASH_PWD"].ToString(), "MD5");
        dsRet = instOrder.GetEmdIDs(Convert.ToInt64(txtOrderNo.Text),Convert.ToInt16(lstTripSn.SelectedValue),2, Application["UID"].ToString(), sign, out ret_code, out ret_msg);
        if (ret_code < 0)
        {
            lblError.Text = ret_msg;
            return;
        }
        if (ret_code > 0)
        {
            switch (ret_code)
            {
                case 91:
                    lblError.Text = "登录错";
                    break;
                default:
                    lblError.Text = "未知错误：" + ret_code.ToString();
                    break;
            }
            return;
        }
        lblError.Text = "";
        ListItem i;
        lstEmdID.Items.Clear();
        foreach (DataRow row2 in dsRet.Tables[0].Rows)
        {
            i = new ListItem();
            i.Text = row2["name"].ToString();
            i.Value = row2["id"].ToString();
            lstEmdID.Items.Add(i);
        }
        dsRet.Clear();
    }
}
